
$(function(){
	productId = sessionStorage['productId'];
	$("#productId").val(productId);
	productName = sessionStorage['productName'];
	productPrice = sessionStorage['productPrice'];
	productImage = sessionStorage['productImage'];
	userName = sessionStorage['username'];
	addressId = sessionStorage['addressId'];
	if(addressId!=null){
		Newtouch.Ajax.sendParams({addressId:addressId},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/buyProduct.jsp.findAddressById",function(data){
			if(data == null){
				$("#userAddress").append('<button type="button" class="btn"  >添加地址</button>');
				
			}else{
				$("#userAddress").append("地址："+data.city_name+data.area_name+data.detail+data.deliveryPersonNm+data.deliveryMobile);
				$("#addressId").val(data.id);
			}
			
		},function(err){
		});
	}else{
	Newtouch.Ajax.sendParams({userName:sessionStorage['username']},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/buyProduct.jsp.findAddressByUserName",function(data){	
		if(data == null){
			$("#userAddress").append('<button type="button" class="btn"  >添加地址</button>');
			
		}else{
			
			$("#userAddress").append("地址："+data.city_name+data.area_name+data.detail+data.deliveryPersonNm+data.deliveryMobile);
			$("#addressId").val(data.id);
			
		}
		
	},function(err){
	});
	}
	$("#productDetail").append('<img src="'+productImage+'" class="img-polaroid"/>'+productName+'<span style="margin-left:120px">'+productPrice+'</span><span id="sum"></span>');
	$("#userAddress").bind("tap",function(){
		androidLocal({"type":3});
		androidLocal({"productId":productId});
		androidLocal({"productName":productName});
		androidLocal({"productPrice":productPrice});
		androidLocal({"image":productImage});
		androidLocal({"comment":$("#comment").val()});
		window.location="chooseAddress.html"
	});
});
$(function(){
	
	    //获得文本框对象
	
	    var t = $("#text_box");
	
	    //数量增加操作
	
	    $("#add").click(function(){    
	
	        t.val(parseInt(t.val())+1)
	
	        setTotal();
	
	    }) 
	
	    //数量减少操作
	
	    $("#min").click(function(){
	
	        t.val(parseInt(t.val())-1);
	
	        if (parseInt(t.val())<=1){
	
			        	$(window).mobileMsgBox({msgText:"数量不能再减啦！",delayTime:500});		
			        	t.val(1);
	
	        }
	
	        setTotal();
	
	    })
	
	    //计算操作
	
	    function setTotal(){
	
	        $("#total").html((parseInt(t.val())*productPrice+8).toFixed(2));//toFixed()是保留小数点的函数很实用哦
	        $("#amount").val((parseInt(t.val())*productPrice+8).toFixed(2));
	    }  
	
	    //初始化
	
	    setTotal();
	    
	    //确认
	    $("#buyProduct").click(function(){
	    	if($("#addressId").val()){
	    	var params = {};
	    	params['userName'] = $("#userName").val();
	    	params['productId'] = $("#productId").val();
	    	params['addressId'] =$("#addressId").val();
	    	params['comment'] =$("#comment").val();
	    	params['amount'] =$("#amount").val();
	    	params['quantity'] =$("#text_box").val();
	    	params['userId'] =sessionStorage['userId'];
	    	Newtouch.Ajax.sendParams(params,localStorage['APP_AJAX_PATH']+"/drinkingBar/view/buyProduct.jsp.buyProduct",function(data){	
	    		 window.location="buyProductSuccess.html";
	    		
	    	},function(err){
	    	});
	    	}else{
	    	 $(window).mobileMsgBox({msgText:"请添加送货地址！",delayTime:500});
	    	}
	    }
	    	)

	})

var androidLocal=function(p){
	for(var k in p){
		sessionStorage[k]=p[k];
	}
	
}


	
	
